import 'package:chat_msg_demo/core/chat_controller.dart';
import 'package:chat_msg_demo/model/message_model.dart';
import 'package:chat_msg_demo/widget/chat_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ChatPage extends StatefulWidget {
  const ChatPage({super.key});

  @override
  State<ChatPage> createState() => _ChatPageState();
}

class _ChatPageState extends State<ChatPage> {
  int count = 0;
  late ChatController chatController;

  final List<MessageModel> _messageList = [
    MessageModel(
      ownerType: OwnerType.receiver,
      content: 'test content',
      createdAt: 1741157569708,
      id: 1,
      ownerName: 'o1'
    ),
    MessageModel(
      ownerType: OwnerType.receiver,
      content: 'test content',
      createdAt: 1741157569708,
      id: 2,
      ownerName: '02'
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('聊天室'),),
      body: Column(
        children: [
          Expanded(child: ChatList(chatController: chatController)),
          Row(
            children: [
              ElevatedButton(
                  onPressed: _loadMore, child: const Text('loadMore')),
              ElevatedButton(onPressed: _send, child: Text('发送'))
            ],
          )
        ],
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    chatController = ChatController(
      initialMessageList: _messageList,
      scrollController: ScrollController(), timePellet: 60,
      messageWidgetBuilder: _diyMessageWidget
    );
  }
  void _loadMore() {
    final List<MessageModel> messageList = [
      MessageModel(
          ownerType: OwnerType.sender,
          content: 'Hello 333',
          createdAt: DateTime.now().millisecondsSinceEpoch,
          avatar: 'https://o.devio.org/images/o_as/avatar/tx2.jpeg',
          ownerName: 'Imooc'),
      MessageModel(
          ownerType: OwnerType.receiver,
          content: 'Nice',
          createdAt: DateTime.now().millisecondsSinceEpoch,
          avatar: 'https://o.devio.org/images/o_as/avatar/tx2.jpeg',
          ownerName: 'ChatGPT')
    ];
    chatController.loadMoreData(messageList);
  }
  void _send() {
    chatController.addMessage(MessageModel(
        ownerType: OwnerType.sender,
        content: 'Hello ${count++}',
        createdAt: DateTime.now().millisecondsSinceEpoch,
        avatar: '',
        ownerName: 'Imooc'));
    // 模拟回应
    Future.delayed(const Duration(milliseconds: 2000), () {
      chatController.addMessage(
        MessageModel(
            ownerType: OwnerType.receiver,
            content: 'Nice',
            createdAt: DateTime.now().millisecondsSinceEpoch,
            avatar: 'https://o.devio.org/images/o_as/avatar/tx2.jpeg',
            ownerName: 'ChatGPT'),
      );
    });
  }

  Widget _diyMessageWidget(MessageModel message) {
    return Container(
      margin: const EdgeInsets.only(top: 10, bottom: 10),
      padding: const EdgeInsets.only(top: 30, bottom: 30),
      decoration: BoxDecoration(
          color: message.ownerType == OwnerType.sender
              ? Colors.amberAccent
              : Colors.redAccent),
      child: Text('${message.ownerName}: ${message.content}'),
    );
  }
}
